package com.awspaas.user.apps.shhtaerospaceindustrial.event;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.actionsoft.bpms.api.OpenApiClient;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.ProcessAPI;

public class CleanInvalidVistorInfoFor149 implements IJob {
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		String apiServer = "https://www.sht808.com/portal/openapi";

		String accessKey = "opapiuser4326";
		String secret = "Abcd123456@#";

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar calendar = new GregorianCalendar();
		Date now = new Date();
		calendar.setTime(now);
		calendar.add(Calendar.DATE, -2);
		Date endDate = calendar.getTime();
		String endTime = endDate.toLocaleString();
		
		calendar.add(Calendar.DATE, -1);
		Date endDate2 = calendar.getTime();
		String endTime2 = endDate.toLocaleString();
		
//		calendar.setTime(now);
//		calendar.add(calendar.DATE, -1);
//		Date endDate1 = calendar.getTime();
//		String endTime1 = endDate.toLocaleString();
		
		Map<String, Object> args = new HashMap<String, Object>();
		OpenApiClient client = new OpenApiClient(apiServer, accessKey, secret);
		
		/***
		 * 查询并删除过期访客自助实例信息
		 */
		System.out.println("Delete useless visitor self info...");
		
		String visitorQuery = "select distinct t.processinstid as ID  from WFH_TASK t where t.processdefid='obj_275bed151e5840d08e10e518644de33a' and t.endtime < to_date('2023-4-30 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processList = new ArrayList<Map<String, Object>>();
		ProcessAPI prosapi=SDK.getProcessAPI();
		
		processList = DBSql.query(visitorQuery, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processList.size(); i++) {
			String id = processList.get(i).get("ID").toString();
			System.out.println("Delete  zz visitor process："+i+"--->"+id);
			boolean isok=prosapi.deleteById(id, "admin");
			System.out.println("Delete  action is :"+isok);
//			args.put("processInstId", id);
//			args.put("uid", "admin");
//			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		/***
		 * 查询并删除过期员工申请访客实例信息
		 */
		System.out.println("Delete useless visitor self info...");
		
		String visitorQuery1 = "select distinct t.processinstid as ID  from WFH_TASK t where t.processdefid='obj_a5d447ce1ef94d418ccbda4eabd766ba' and t.endtime < to_date('2023-5-20 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processList1 = new ArrayList<Map<String, Object>>();
		
		
		processList1 = DBSql.query(visitorQuery1, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processList1.size(); i++) {
			String id = processList1.get(i).get("ID").toString();
			System.out.println("Delete  psn app visitor process："+i+"--->"+id);
			boolean isok=prosapi.deleteById(id, "admin");
			System.out.println("Delete  action is :"+isok);
//			args.put("processInstId", id);
//			args.put("uid", "admin");
//			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		
		/***
		 * 查询并删除过期149访客自助实例信息
		 */
		System.out.println("Delete useless visitor self info...");
		
		String visitorQuery149 = "select distinct t.processinstid as ID  from WFH_TASK t where t.processdefid='obj_52e4446bb3c04b088fae1605a17bccbb' and t.endtime < to_date('2023-5-20 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processList149 = new ArrayList<Map<String, Object>>();
		
		processList149 = DBSql.query(visitorQuery149, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processList149.size(); i++) {
			String id = processList149.get(i).get("ID").toString();
			System.out.println("Delete  149zz visitor process："+i+"--->"+id);
			boolean isok=prosapi.deleteById(id, "admin");
			System.out.println("Delete  action is :"+isok);
//			args.put("processInstId", id);
//			args.put("uid", "admin");
//			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		

	}

}
